package com.sdhs.workflow.flowable.listener;

import com.sdhs.common.utils.spring.SpringUtils;
import com.sdhs.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.flowable.engine.impl.el.FixedValue;

import java.util.Collections;
import java.util.List;

/**
 * 安全部门角色监听器
 */
@Slf4j
public class RolePromoterCompanyExecutionListener implements ExecutionListener {
    // 角色keys
    private FixedValue roleKeys;
    // 变量名称
    private FixedValue varName;

    @Override
    public void notify(DelegateExecution execution) {
        ISysUserService userService = SpringUtils.getBean(ISysUserService.class);
        //角色集合
        String roleKeyGroup = (String) roleKeys.getValue(execution);
        //根据角色获取当前公司下人员userIds
        List<String> userIds = userService.selectUserIdsByRoleKeysAndCompany(Collections.singletonList(roleKeyGroup));
        //设置变量
        execution.setVariable((String) varName.getValue(execution), userIds);
    }


    // setter方法用于接收XML配置的参数
    public void setRoleKeys(FixedValue roleKeys) {
        this.roleKeys = roleKeys;
    }

    // setter方法用于接收XML配置的参数
    public void setVarName(FixedValue varName) {
        this.varName = varName;
    }

}
